package cn.cqie.attendance.student.sevice.impl;



import cn.cqie.attendance.common.util.Page;
import cn.cqie.attendance.student.domian.*;
import cn.cqie.attendance.student.mapper.StudentLeaveMapper;
import cn.cqie.attendance.student.sevice.IStudentLeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 学生请假service业务层
 *
 * @author lfj
 */
@Service
@Transactional
public class StudentLeaveServiceImpl implements IStudentLeaveService {

    @Autowired
    private StudentLeaveMapper studentLeaveMapper;

    /**
     * 查询当前学生的所有的记录
     *
     * @return 请假集合
     */
    @Override
    public Page<Leaves> queryLeaves(QueryLeaves queryLeaves) {
        int startPage = 0;
        int endPage = 10;
        if (queryLeaves.getPageCurrent() == null || queryLeaves.getPageSize() == null) {
            startPage = 0;
            endPage = 10;
        }
        if (queryLeaves.getPageCurrent() != null) {
            startPage = (queryLeaves.getPageCurrent() - 1) * queryLeaves.getPageSize();
            endPage = queryLeaves.getPageSize() * queryLeaves.getPageCurrent();
        }
//        Long stuId = CurrentInfoUtil.getUserId();
//        queryLeaves.setStuId(stuId);
        List<Leaves> leaves = studentLeaveMapper.queryLeaves(queryLeaves);
        if (null == leaves || leaves.size() == 0) {
            return Page.INFOERROR;
        }
        return new Page<>(200, "Success", startPage, endPage, leaves.size(), leaves);
    }

    /**
     * 学生请假详情
     *
     * @param id    请假id
     * @param stuId 学生id
     * @return
     */
    @Override
    public Page queryLeaveDetail(Long id, Long stuId) {
        List<Student> students = studentLeaveMapper.queryLeaveDetail(id, stuId);
        int total = students.size();
        if (null == students || total == 0) {
            return Page.INFOERROR;
        }
        return new Page(200, "Success", students);
    }

    /**
     * 学生请假日志
     *
     * @param id    请假id
     * @param stuId 学生id
     * @return
     */
    @Override
    public Page<LeaveLog> queryLeaveLog(Long id, Long stuId) {
        List<LeaveLog> leaveLogs = studentLeaveMapper.queryLeaveLog(id, stuId);
        int total = leaveLogs.size();
        if (null == leaveLogs || total == 0) {
            return Page.INFOERROR;
        }
        return new Page<>(200, "Success", leaveLogs);
    }

    /**
     * 新增请假
     *
     * @param addLeaves 请假信息
     * @return
     */
    @Override
    public int addLeave(AddLeaves addLeaves) {
        studentLeaveMapper.addLeave(addLeaves);
        return addLeaves.getId();
    }

    /**
     * 修改请假
     *
     * @param addLeaves 请假信息
     * @return
     */
    @Override
    public Page updateLeave(AddLeaves addLeaves) {
        int i = studentLeaveMapper.updateLeave(addLeaves);
        if (i>0) {
            return Page.SUCCESS;
        }else {
            return Page.FAIL;
        }
    }

    /**
     * 查询请假学生
     *
     * @param id 学生id
     * @return
     */
    @Override
    public Page queryStudent(Long id) {

        List<Student> studentList = studentLeaveMapper.queryStudent(id);
        int total = studentList.size();
        if (null == studentList || total == 0) {
            return Page.INFOERROR;
        }
        return new Page(200, "Success", studentList);
    }

    /**
     * 查询附件地址
     *
     * @param id 请假id
     * @return
     */
    @Override
    public Page queryAnnex(Long id) {
        List<Attachment> attachmentList = studentLeaveMapper.queryAnnex(id);
        int total = attachmentList.size();
        if (null == attachmentList || total == 0) {
            return Page.INFOERROR;
        }
        return new Page(200, "Success", attachmentList);
    }

    /**
     * 查询新增记录的id
     * @return
     */
    @Override
    public Integer queryLeavesId() {
        int id = studentLeaveMapper.queryLeavesId();
        return id;
    }

    /**
     * 新增请假日志
     * @return
     */
    @Override
    public Page addLeaveLog(AddLeaveLog addLeaveLog) {
        int i = studentLeaveMapper.addLeaveLog(addLeaveLog);
        if (i > 0) {
            return Page.SUCCESS;
        } else {
            return Page.FAIL;
        }
    }
}
